package com.web.exiaodao.remote.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.web.exiaodao.remote.service.ApiUserService;
import com.web.webstart.base.util.Validator;
import com.web.webstart.base.util.XaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.exiaodao.remote.service.ApiFestivalRemindService;
import com.web.exiaodao.remote.vo.FestivalRemindVo;
import com.web.exiaodao.business.entity.FestivalRemind;

/**
 * @author eason.zt
 * @version V1.0
 * @Title: ApiFestivalRemindController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 节日提醒信息接口
 * @date 2015年3月23日 下午1:00:00
 */
@Api(value = "FestivalRemind", description = "节日提醒(生日,结婚纪念日,忌日)接口", position = 10)
@Controller
@RequestMapping("/api/festivalRemind")
public class ApiFestivalRemindController extends BaseController {


    @Autowired
    private ApiUserService userService;
    @Autowired
    private ApiFestivalRemindService festivalRemindService;

    /**
     * @return
     * @Title: findFestivalRemindList
     * @Description: 查询所有节日提醒信息
     */
    @ApiOperation(value = "查询所有节日提醒", notes = "查询所有节日提醒信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findFestivalRemindList", method = RequestMethod.POST)
    public XaResult<List<FestivalRemindVo>> findFestivalRemindList(
            @ApiParam("用户id,字段名:id") @RequestParam(value = "userId") Long userId,
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("类别,节日类别（1：生日 2:结婚纪念日 3：忌日）") @RequestParam(required = false) Integer type
    ) throws BusinessException {
        XaResult<List<FestivalRemindVo>> xr = new XaResult<>();
        if(XaUtil.isEmpty(userId)){
            xr.error("用户Id不能为空！");
            return xr;
        }
        if (XaUtil.isEmpty(userService.validUserId(userId+""))) {
            xr.error("用户Id不存在！");
            return xr;
        }
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = null;
        if (XaUtil.isNotEmpty(type)) {
            filterParams = WebUitl.getParametersStartingWith(
                    "{'search_EQ_type':'"+ type+"'" +
                            ",'search_EQ_userId':'"+userId+"'}", "search_");
        } else {
            filterParams = WebUitl.getParametersStartingWith("{'search_EQ_userId':'" + userId + "'}", "search_");
        }
        return festivalRemindService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
    }

    /**
     * @return
     * @Title: save
     * @Description: 新增一条实体
     */
    @ApiOperation(value = "新增节日提醒", notes = "供前端前期填充数据测试使用,上线后删除;新增节日提醒,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "saveFestivalRemind", method = RequestMethod.POST)
    public XaResult<FestivalRemindVo> saveFestivalRemind(
            @ApiParam("用户id,字段名:id") @RequestParam(value = "userId") Long userId,
            @ApiParam("姓名,字段名:name") @RequestParam(value = "name") String name,
            @ApiParam("手机,字段名:cellphone") @RequestParam(value = "cellphone") String cellphone,
            @ApiParam("日期,字段名:festivalDate;（yyyy-mm-dd）") @RequestParam(value = "festivalDate") String festivalDate,
            @ApiParam("农历，公历;是否是农历（1：农历 0：公历）,字段名:isLunarCalendar") @RequestParam(value = "isLunarCalendar") Integer isLunarCalendar,
            @ApiParam("提前提醒天数,字段名:daysDiff") @RequestParam(value = "daysDiff", required = false, defaultValue = "22") Integer daysDiff,
            @ApiParam("节日类别（1：生日 2:结婚纪念日 3：忌日）,字段名:type") @RequestParam(value = "type", defaultValue = "1") Integer type,
            HttpServletRequest request
    ) throws BusinessException {
        XaResult<FestivalRemindVo> xr = new XaResult<>();
        if(XaUtil.isEmpty(userId)){
            xr.error("用户Id不能为空！");
            return xr;
        }
        if (XaUtil.isEmpty(userService.validUserId(userId+""))) {
            xr.error("用户Id不存在！");
            return xr;
        }
        if (XaUtil.isEmpty(name)) {
            xr.error("姓名不能为空");
            return xr;
        }
        if (XaUtil.isEmpty(cellphone)) {
            xr.error("手机不能为空");
            return xr;
        }
        if (!Validator.isMobile(cellphone)) {
            xr.error("手机号码格式不正确");
            return xr;
        }
        if (XaUtil.isEmpty(festivalDate)) {
            xr.error("日期不能为空");
            return xr;
        }
        if (!Validator.isDate(festivalDate)) {
            xr.error("日期格式不正确(YYYY-MM-dd)");
            return xr;
        }
        if (XaUtil.isEmpty(isLunarCalendar)) {
            xr.error("是否是农历不能为空");
            return xr;
        }
        if (!(isLunarCalendar== 0 || isLunarCalendar == 1)) {
            xr.error("是否是农历只能为0和1");
            return xr;
        }
        if (XaUtil.isEmpty(daysDiff)) {
            xr.error("提前提醒天数不能为空");
            return xr;
        }
        if (daysDiff< 0) {
            xr.error("提前提醒天数不能小于0");
            return xr;
        }
        if (daysDiff > 365) {
            xr.error("提前提醒天数不能大于1年");
            return xr;
        }
        if (XaUtil.isEmpty(type)) {
            xr.error("节日类别不能为空");
            return xr;
        }
        FestivalRemind model = new FestivalRemind();
        model.setName(name);
        model.setCellphone(cellphone);
        model.setFestivalDate(festivalDate);
        model.setIsLunarCalendar(isLunarCalendar);
        model.setDaysDiff(daysDiff);
        model.setType(type);
        model.setUserId(userId);
        return festivalRemindService.createModel(model);
    }

    /**
     * @return
     * @Title:
     * @Description: 修改一条实体
     */
    @ApiOperation(value = "修改节日提醒", notes = "修改节日提醒,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "updateFestivalRemind", method = RequestMethod.POST)
    public XaResult<FestivalRemindVo> updateFestivalRemind(
            @ApiParam("唯一编号,字段名:tId") @RequestParam(value = "tId") Long tId,
            @ApiParam("姓名,字段名:name") @RequestParam(value = "name", required = false) String name,
            @ApiParam("手机,字段名:cellphone") @RequestParam(value = "cellphone", required = false) String cellphone,
            @ApiParam("日期,字段名:festivalDate（yyyy-mm-dd）") @RequestParam(value = "festivalDate", required = false) String festivalDate,
            @ApiParam("农历，公历;是否是农历（1：农历 0：公历）,字段名:isLunarCalendar") @RequestParam(value = "isLunarCalendar", required = false) Integer isLunarCalendar,
            @ApiParam("提前提醒天数,字段名:daysDiff") @RequestParam(value = "daysDiff", required = false, defaultValue = "22") Integer daysDiff,
            @ApiParam("节日类别（1：生日 2:结婚纪念日 3：忌日）,字段名:type") @RequestParam(value = "type", required = false, defaultValue = "1") Integer type,
            HttpServletRequest request
    ) throws BusinessException {
        XaResult<FestivalRemindVo> xr = new XaResult<>();
        if (XaUtil.isNotEmpty(cellphone)) {
            if (!Validator.isMobile(cellphone)) {
                xr.error("手机号码格式不正确");
                return xr;
            }
        }
        if (XaUtil.isNotEmpty(festivalDate)) {
            if (!Validator.isDate(festivalDate)) {
                xr.error("日期格式不正确(YYYY-MM-dd)");
                return xr;
            }
        }
        if (XaUtil.isNotEmpty(isLunarCalendar)) {
            if (!(isLunarCalendar == 0 || isLunarCalendar == 1)) {
                xr.error("是否是农历只能为0和1");
                return xr;
            }
        }

        if (XaUtil.isEmpty(daysDiff)) {
            if (daysDiff < 0) {
                xr.error("提前提醒天数不能小于0");
                return xr;
            }
            if (daysDiff > 365) {
                xr.error("提前提醒天数不能大于1年");
                return xr;
            }
            return xr;
        }
        if (XaUtil.isEmpty(type)) {
            xr.error("节日类别不能为空");
            return xr;
        }
        FestivalRemind model = new FestivalRemind();
        model.setName(name);
        model.setCellphone(cellphone);
        model.setFestivalDate(festivalDate);
        model.setIsLunarCalendar(isLunarCalendar);
        model.setDaysDiff(daysDiff);
        model.setType(type);
        model.setId(tId);
        return festivalRemindService.updateModel(model);
    }

    /**
     * @param status 操作类型:-1锁定,0无效,1正常,2发布,3删除,默认删除操作
     * @return
     * @Title: operateFestivalRemindById
     * @Description: 操作一个实体状态, 根据status进行操作
     */
    @ApiOperation(value = "根据ID修改节日提醒状态", notes = "修改节日提醒状态,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "operateFestivalRemindById", method = RequestMethod.POST)
    public XaResult<FestivalRemindVo> operateFestivalRemindById(
            HttpServletRequest request,
            @ApiParam("编号,字段名:modelIds,必填") @RequestParam(value = "modelId") String modelIds,
            @ApiParam("操作类型,字段名:status,-1锁定,0无效,1正常,2发布,3删除,选填,默认删除操作") @RequestParam(defaultValue = "3") Integer status
    ) throws BusinessException {
        return festivalRemindService.multiOperate(modelIds, status);
    }
}

